<template>
	 <ex-dialog v-model="visible" draggable :title="form.id ? '编辑盘点单' : '新增盘点单'"
	:width=700 :contentHeight=450>
	  <template #default>
		  <el-row :gutter="80" style="margin-top: 10px">
			  <el-col :lg="24">
				  <el-form :model="form" :rules="button_rules" ref="dialogForm" label-width="100px" label-position="left">
					  <template v-if="form.id">
						  <el-form-item label="盘点单编号" prop="code">
							  <el-input v-model="form.code" placeholder="盘点单编号" disabled></el-input>
						  </el-form-item>
						  <el-form-item label="盘点部门" prop="orgId">
							  <ex-label :key="form.orgId" code="SysOrganization" :value="form.orgId"/>
						  </el-form-item>
						  <el-form-item label="制定人" prop="createUser">
							  <ex-label :key="form.createUser" code="SysUser" :value="form.createUser"/>
						  </el-form-item>
						  <el-form-item label="制定时间" prop="createTime">
							  <ex-label :key="form.createTime" format code="DATE" :value="form.createTime"/>
						  </el-form-item>
					  </template>
					  <template v-else>
						  <el-form-item label="盘点部门" prop="orgId">
							  <ex-selector :params="{ids:this.filterDeptIds}" :model="form" prop="orgId" code="SysOrganization" :isicon="true"/>
						  </el-form-item>
						  <el-form-item label="制定人" prop="createUser">
							  <ex-label :key="loginUser.id" code="SysUser" :value="loginUser.id"/>
						  </el-form-item>
						  <el-form-item label="制定时间" prop="createTime">
							  <ex-label :key="nowDate" format code="DATETIME" :value="nowDate"/>
						  </el-form-item>
					  </template>
					  <el-form-item label="盘点单名称" prop="name">
						  <el-input v-model="form.name" clearable placeholder="盘点单名称"></el-input>
					  </el-form-item>
					  <el-form-item label="描述" prop="description">
						  <el-input v-model="form.description" :rows="3" type="textarea" clearable placeholder="描述"></el-input>
					  </el-form-item>
				  </el-form>
			  </el-col>
		  </el-row>
	  </template>
	  <template #footer>
		<div style="flex: auto;text-align: center">
		  <el-button plain type="primary" @click="save" :loading="loading">保 存</el-button>
		  <el-button plain type="warning" @click="reset" :loading="loading">重 置</el-button>
		  <el-button plain type="info" @click="this.visible = false" :loading="loading">关 闭</el-button>
		</div>
	  </template>

	</ex-dialog>
  </template>

  <script>
  import scIconSelect from '@/components/scIconSelect'
  import ExRelTable from "@/components/exRelTable/index.vue";
  import ExLabel from "@/components/exLabel/index.vue";
  import ExSelect from "@/components/exSelect/index.vue";
  import exDialog from '@/components/exDialog';
  import ExTreeTable from "@/components/exTreeTable/index.vue";
  import moment from "moment/moment";
  import tool from "@/utils/tool";
  import ExSelector from "@/components/exSelector/index.vue";
  import entity from "@/constant/model/entity";

  export default {
	emits: ['success'],
	components: {
		ExSelector,
		ExTreeTable,
		ExSelect,
		ExLabel,
	  ExRelTable,
	  scIconSelect,
	  exDialog
	},
	props: {
	  menu: {
		type: Object, default: () => {
		}
	  },
	},
	data() {
	  return {
		visible: false,
		  entityName: "FamsAssetCheckTask",
		form: {
			id: "",
			name: "",
			code: "",
			orgId: "",
			createTime: "",
			createUser: "",
			description:"",
		},
		button_rules: {
			orgId: [{required: true, message: "请选择盘点部门", trigger: "blur"},],
			name: [{required: true, message: "请输入盘点单名称", trigger: "blur"},],
		},
		loading: false,
		  filterDeptIds : [],
		  loginUser : tool.current.user(),
		  nowDate : moment(new Date()).format("YYYYMMDD"),
		oldForm: null,
	  }
	},
	  mounted() {

	},
	  onLoad() {},
	methods: {
		reset() {
			this.form = this.$TOOL.objCopy( this.oldForm);
			this.$refs.roleRelTable.refresh();
		},
		//显示
		open(entity) {
			this.form = {};
			this.filterDeptIds = [];
			this.form = Object.assign({}, entity);
			this.oldForm = this.$TOOL.objCopy( this.form);
			//获取盘点任务关联部门
			let queryDto = {
				firstEntity: this.$CONSTANT.entity.FamsAssetCheckTask.name,
				firstEntityId: this.form.taskId,
				secondEntity: "ManageDept",
			};
			this.$API.system.relMap.load.post(queryDto).then(rsp => {
				if (rsp.code === 2000 && Array.isArray(rsp.data)) {
					this.filterDeptIds = rsp.data.map(item => {
						return item.secondEntityId;
					});
					this.visible = true;
				}
			});
		},
		//保存
		async save() {
			let valid  = await this.$refs.dialogForm.validate( valid => {return valid;});
			if (valid) {
				try {
					this.loading = true
					if(!this.form.id){
						this.form.createTime = moment(new Date()).format("YYYYMMDDHHmmss");
					}
					var res = await this.$API.asset.checkDocumentApi.save.post(this.form)
					if (res.code === 2000) {
						this.$emit('success');
						this.$message.success(this.$t("code."+res.code));
						this.visible = false;
						this.$store.dispatch("commit",this.form.entityName);
					}
				} finally {
					this.loading = false
				}
			}
		},
	}
  }
  </script>

  <style scoped>
  h2 {
	font-size: 17px;
	color: #3c4a54;
	padding: 0 0 30px 0;
  }

  [data-theme="dark"] h2 {
	color: #fff;
  }
  .label-center{
	  display: flex;
	  justify-content: center;
	  align-items: center;
  }
  </style>
